<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $blog = getBlog();
    $type_list = getBlogType();

} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    saveBlog();
}

// 根据博客编号查询博客信息
function getBlog()
{
    //展示待编辑数据
    //根据id查询博客信息
    $id = $_GET['id'];
    $con = mysqli_connect('localhost', 'root', 'root');
    if (!$con) {
        die('连接失败' . mysqli_connect_error());
    }
    mysqli_select_db($con, 'blog');
    mysqli_set_charset($con, 'utf8');
    $sql = "select * from blog where id=$id";
    $result = mysqli_query($con, $sql);
    //因为只有一条数据
    $blog = mysqli_fetch_assoc($result);
    mysqli_close($con);
    return $blog;
}

//查询所有博客类别
function getBlogType()
{
    //查询所有类别
    $con = mysqli_connect('localhost', 'root', 'root');
    if (!$con) {
        die('连接失败' . mysqli_connect_error());
    }
    mysqli_select_db($con, 'blog');
    mysqli_set_charset($con, 'utf8');
    $sql = "select * from blog_type order by type_id desc";
    $result = mysqli_query($con, $sql);
    $type_list = [];
    while ($row = mysqli_fetch_assoc($result)) {
        $type_list[] = $row;
    }
    mysqli_close($con);
    return $type_list;
}

//保存修改
function saveBlog()
{
    //获取表单空间的值
    $id = $_POST['id'];
    $title = $_POST['title'];
    $content = $_POST['content'];
    $blogtype_id = $_POST['blogtype_id'];
    $con = mysqli_connect('localhost', 'root', 'root');
    if (!$con) {
        die('连接失败' . mysqli_connect_error());
    }
    mysqli_select_db($con, 'blog');
    mysqli_set_charset($con, 'utf8');
    $sql = "update blog set title='$title',content='$content',blogtype_id=$blogtype_id where id=$id";
    $result = mysqli_query($con, $sql);
    if (!$result) {
        echo '保存失败' . mysqli_error($con);
    } else {
        header('Location:blog_list.php');
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发表博客</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .box {

            margin-top: 20px;
        }

        textarea.form-control {
            height: 450px;
        }


    </style>
</head>
<body>
<div class="container box">
    <a href="blog_list.php">返回</a>
    <form class="form-horizontal" action="blog_edite.php" method="post">
        <input type="hidden" name="id" value="<?php echo $blog['id'] ?>">
        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">标题</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" value="<?php echo $blog['title'] ?>"
                       id="title" name="title" placeholder="输入标题名称">
            </div>
        </div>
        <div class="form-group">
            <label for="content" class="col-sm-2 control-label">内容</label>
            <div class="col-sm-10">
                <textarea name="content" id="content" class="form-control"><?php echo $blog['content'] ?></textarea>
            </div>
        </div>
        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">所属类别</label>
            <div class="col-sm-10">
                <select name="blogtype_id" id="" class="form-control">
                    <?php
                    foreach ($type_list as $key => $value) {
                        if ($value['type_id'] == $blog['blogtype_id']) {
                            ?>
                            <option selected="selected"
                                    value="<?php echo $value['type_id'] ?>"><?php echo $value['type_name'] ?></option>
                        <?php } else {
                            ?>
                            <option
                                    value="<?php echo $value['type_id'] ?>"><?php echo $value['type_name'] ?></option>
                        <?php }
                    } ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-4 col-sm-10">
                <button type="submit" class="btn btn-default">保存</button>
            </div>
        </div>
    </form>
</div>

</body>
</html>
